<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\UploadForm;
use yii\web\UploadedFile;
use app\models\ExcelReader;
use app\models\Huoke;
use app\helpers\Html;
use yii\helpers\Url;

class SiteController extends Controller
{

    public $enableCsrfValidation = false;

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        $page = Yii::$app->request->get('page',1);
        $status = Yii::$app->request->get('status',0);
        $keyword = Yii::$app->request->get('keyword');
        $limit = 30;

        $offset = ($page-1)*$limit;
        $huokes = Huoke::find();

        $huokes->offset($offset)->limit($limit);

        $huokes->where("1=1");
        if($status == 1)
        {
            $huokes->andWhere("status=0");
        }
        if($status == 2)
        {
            $huokes->andWhere("status>0");
        }
        if($status == 3)
        {
            $huokes->andWhere("status=-1");
        }
        if($status == 4)
        {
            $huokes->andWhere("approved=0");
        }
        else
        {
            $huokes->andWhere("approved=1");
        }

        if($keyword)
        {
            $huokes->andWhere(array(
                'or',
                array('like','name',$keyword),
                array('like','teleNum',$keyword)
            ));
        }

        $count = $huokes->count();

        $items = $huokes->orderBy('id desc')->all();

        $totalPage = ceil($count/$limit);
        $pagination = Html::makePagination($page,$totalPage,'site/index',array());

        return $this->render('index', array(
            'huokes'=>$items,
            'pagination'=>$pagination,
        ));
    }

    public function actionApprove()
    {
        $ids = $page = Yii::$app->request->post('ids');
        foreach($ids as $id)
        {
            $huoke = Huoke::findOne($id);
            $huoke->setAttribute('approved',1);
            $huoke->save();
        }
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionUpload()
    {
        return $this->render('upload');
    }

    public function actionUploadpost()
    {
        $model = new UploadForm();
        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, 'file');
            if ($model->file && $model->validate()) {
                $path = 'uploads/' . $model->file->baseName . '.' . $model->file->extension;
                if($model->file->saveAs($path))
                {
                    $reader = new ExcelReader();
                    $reader->load($path);
                    $data = $reader->getData();
                    $total = $reader->save();
                    Yii::$app->getSession()->setFlash('message', "导入数据{$total}条!");
                    $this->redirect(Url::toRoute('site/index'));
                }
            }
        }
        //echo 'uploads/' . $model->file->baseName . '.' . $model->file->extension;
        //return $this->render('upload', ['model' => $model]);
    }
}
